package gbench.sandbox.record.xls;

import org.junit.jupiter.api.Test;

import java.util.stream.*;

import gbench.common.tree.LittleTree;
import gbench.commonApp.data.DataApp;
import gbench.commonApp.data.DataMatrixApp;
import static gbench.common.tree.LittleTree.compose_cs;
import static gbench.common.tree.LittleTree.RPTA;
import static gbench.common.tree.LittleTree.cph2;
import static gbench.common.tree.LittleTree.MFT;
import static gbench.common.tree.LittleTree.IRecord.A;
import static gbench.common.fs.XlsFile.SimpleExcel;

/**
 * excel 输入excel的演示demo
 * @author gbench
 *
 */
public class JunitWriteExcel extends DataMatrixApp{
   
    @Test
    public void foo() {// 写入一段数据
        final var mx = cph2(RPTA(4, A(1, 0, -1))).collect(omc(SPLIT("ABCD", "")));
        final var excel = new SimpleExcel("C:/Users/gbench/Desktop/abc.xlsx");
        excel.write("mysheet!A1", mx).save();
        excel.close();
        System.out.println("书写结束");
    }
    
    @Test
    public void bar() {// 写入一段数据
        final var mx = cph2(RPTA(4,A(1,0,-1))).collect(dmc(SPLIT("ABCD","")));// 生成   EXCEL格式的名称 前面电邮一个  下划线的“_”
        final var excel =  new SimpleExcel("C:/Users/gbench/Desktop/abc.xlsx"); // 构造一个EXCEL对象
        Stream.iterate(0, i->i+1).limit(10).forEach(i->excel.write(MFT("mysheet{0}!A1",i), mx)); //  书写到指定sheet
        Stream.of(excel).forEach(compose_cs(SimpleExcel::close,SimpleExcel::save)); // 关闭 excel
        System.out.println("书写结束");
    }
    
    /**
     * 数据库的数据表导出成excel
     */
    @Test
    public void qux() {
        final var excel = new SimpleExcel("F:/slicef/desktop/doc/abc.xlsx"); // 构造一个EXCEL对象
        final var dataApp = new DataApp() {
        }.reload2("url","jdbc:mysql://localhost:3306/graph?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=True");
        dataApp.withTransactionX(DataMatrixApp.objmxclc(), sess -> {
            Stream.of("select * from t_vertex", "select * from t_edge").map(LittleTree.kvp_int()).forEach(e -> {
                final var mx = LittleTree.trycatch((String sql) -> sess.sql2u(sql)).apply(e._2());
                final var shtname = MFT("mysheet{0}!A1", e._1());
                println(mx);
                println(shtname);
                excel.write(shtname, mx);
            });
        });
        Stream.of(excel).forEach(compose_cs(SimpleExcel::close,SimpleExcel::save)); // 关闭 excel
        System.out.println("书写结束");
    }

}
